/*
 * YAJHFC - Yet another Java Hylafax client
 * Copyright (C) 2005-2011 Jonas Wolz <info@yajhfc.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Linking YajHFC statically or dynamically with other modules is making 
 *  a combined work based on YajHFC. Thus, the terms and conditions of 
 *  the GNU General Public License cover the whole combination.
 *  In addition, as a special exception, the copyright holders of YajHFC 
 *  give you permission to combine YajHFC with modules that are loaded using
 *  the YajHFC plugin interface as long as such plugins do not attempt to
 *  change the application's name (for example they may not change the main window title bar 
 *  and may not replace or change the About dialog).
 *  You may copy and distribute such a system following the terms of the
 *  GNU GPL for YajHFC and the licenses of the other code concerned,
 *  provided that you include the source code of that other code when 
 *  and as the GNU GPL requires distribution of source code.
 *  
 *  Note that people who make modified versions of YajHFC are not obligated to grant 
 *  this special exception for their modified versions; it is their choice whether to do so.
 *  The GNU General Public License gives permission to release a modified 
 *  version without this exception; this exception also makes it possible 
 *  to release a modified version which carries forward this exception.
 */
package yajhfc.launch;

import gnu.getopt.LongOpt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import yajhfc.VersionInfo;

/**
 * Auto-generates the man page on UNIX
 * 
 * @author jonas
 *
 */
public class ManPrinter extends HelpPrinter {
    // Original documentation in man page header:
    //    .\" First parameter, NAME, should be all caps
    //    .\" Second parameter, SECTION, should be 1\-8, maybe w/ subsection
    //    .\" other parameters are allowed: see man(7), man(1)
    //    .TH YAJHFC 1 "December 25, 2009"
    //    .\" Please adjust this date whenever revising the manpage.
    //    .\"
    //    .\" Some roff macros, for reference:
    //    .\" .nh        disable hyphenation
    //    .\" .hy        enable hyphenation
    //    .\" .ad l      left justify
    //    .\" .ad b      justify to both left and right margins
    //    .\" .nf        disable filling
    //    .\" .fi        enable filling
    //    .\" .br        insert line break
    //    .\" .sp <n>    insert n+1 empty lines
    //    .\" for manpage\-specific macros, see man(7)
    //    .SH NAME
    //    yajhfc \- Yet Another Java HylaFAX Client
    //    .SH SYNOPSIS
    //    .B yajhfc
    //    .RI [ options ] " [documents to send]" ...
    //    .SH DESCRIPTION
    //    This manual page documents the
    //    .B yajhfc
    //    command.
    //    .PP
    //    .\" TeX users may be more comfortable with the \fB<whatever>\fP and
    //    .\" \fI<whatever>\fP escape sequences to invode bold face and italics,
    //    .\" respectively.
    
    public String commandName = "yajhfc";
    public InputStream READMEstream = ManPrinter.class.getResourceAsStream("/README.txt");
    
    public String[] seeAlsoList = {
            "cyajhfc (1)",
            "hylafax (1)",
            "sendfax (1)",
            "java (1)",
    };
    
    public String[] docLocations = {
            "/usr/share/doc/yajhfc",
    };
    
    public String additionalSynopsis = 
        "This command starts the graphical user interface. For a command line only interface, please see\n.B cyajhfc\n."; 
    
    protected void printManHeader(PrintWriter out) throws Exception {
        Date now = new Date();
        out.append(
        ".\\\" This man page has been auto-generated by " + VersionInfo.AppShortName + " " + VersionInfo.AppVersion + " at " + now + "\n"+
        ".TH YAJHFC 1 \"" + (new SimpleDateFormat("MMMM d, yyyy").format(now)) + "\" \"" + VersionInfo.AppShortName + " " + VersionInfo.AppVersion + "\"\n"+
        ".SH NAME\n"+
        commandName + " \\- Yet Another Java HylaFAX Client\n"+
        ".SH SYNOPSIS\n"+
        ".B "+commandName+"\n"+
        ".RI [ options ] \" [documents to send]\" ...\n"+
        ".SH DESCRIPTION\n"+
        "This manual page documents the\n"+
        ".B "+commandName+"\n"+
        "command.\n"+
        ".PP\n"+
        "\\fBYajHFC\\fP is a client for the \\fBHylaFAX\\fP fax server written in Java.\n"+
        ".sp 2\n"+
        "\\fIFeatures:\\fP\n");
        
        // Read feature list from README.txt
        BufferedReader r = new BufferedReader(new InputStreamReader(READMEstream));
        
        String line;
        // Search for "Features:" line:
        while ((line = r.readLine()) != null && !"Features:".equalsIgnoreCase(line.trim())) {};
        if (line == null)
            throw new Exception("Error: \"Features:\" line not found in README.txt!");
        
        // Read the Features lines until we find an empty line
        while ((line = r.readLine()) != null && 
                (line = line.trim()).length() > 0) {
            if (line.startsWith("*"))
                out.append(".br\n");
            out.append(' ').append(quoteMinus(line)).append('\n');
        }
        r.close();
        
        if (additionalSynopsis != null) {
            out.append(".sp 2\n");
            out.append(additionalSynopsis);
            out.append("\n");
        }
        
//        ".br\n"+
//        " * Faxing documents in PostScript, PDF and various other formats\n"+
//        ".br\n"+
//        " * Polling faxes\n"+
//        ".br\n"+
//        " * Support for generating cover pages from templates\n"+
//        ".br\n"+
//        " * Viewing sent and received faxes\n"+
//        ".br\n"+
//        " * Phone book (entries can optionally be read from SQL databases or LDAP directories)\n"+
//        ".br\n"+
//        " * Visible table columns may be selected in a dialog\n"+
//        ".br\n"+
//        " * Supports nine languages: English, French, German, Greek, Italian, Polish, Russian, Spanish and Turkish\n"+
//        ".br\n"
        out.append(".SH OPTIONS\n");
    }
    
    
  
    
    private static final Pattern minus     = Pattern.compile("[\\-\\\\]");
    private static final String  minusRepl = "\\\\$0";
    private static final String quoteMinus(String s) {
        return minus.matcher(s).replaceAll(minusRepl);
    }
    
    private static final Pattern bar       = Pattern.compile("|", Pattern.LITERAL);
    private static final String  barRepl   = Matcher.quoteReplacement("\\fP|\\fI");
    private static final String quoteArgDesc(String s) {
        return bar.matcher(quoteMinus(s)).replaceAll(barRepl);
    }
    
    private static final Pattern[] descPatterns = {
        minus,
        Pattern.compile("(\\\\-\\\\-[a-zA-Z0-9\\-_]+)"), //Match references to other command line parameters
        Pattern.compile("_(.+?)_"), // Match _underlined_ text
        
    };
    private static final String[] descReplacements = {
        minusRepl,
        "\\\\fB$1\\\\fP",
        "\\\\fI$1\\\\fP",
        
    };
    private static final String quoteDescription(String s) {
        String res = s;
        for (int i=0; i<descPatterns.length; i++) {
            res = descPatterns[i].matcher(res).replaceAll(descReplacements[i]);
        }
        return res;
    }
    
    /**
     * Prints usage information in man page format
     * @param out
     * @throws IOException 
     */
    public void printManPage(PrintWriter out, LongOpt[] options) throws Exception {
        options = filterOptions(options);
        
        printManHeader(out);
        for (int i = 0; i < options.length; i++) {
            final LongOpt option = options[i];
            out.println(".TP");
            
            out.print(".B ");
            if (option.getVal() >= MIN_OPT_VAL) {
                out.append("\\-");
                out.append((char)option.getVal());
            }
            if (option.getName() != null) {
                if (option.getVal() < MIN_OPT_VAL) {
                    out.append("  \\-\\-");
                } else {
                    out.append(", \\-\\-");
                }
                out.append(quoteMinus(option.getName()));
                
                final int hasArg = option.getHasArg();
                if (hasArg != LongOpt.NO_ARGUMENT) {
                    out.append("\\fP");
                    if (hasArg == LongOpt.OPTIONAL_ARGUMENT) {
                        out.append('[');
                    }
                    out.append("=\\fI");
                    out.append(quoteArgDesc(getArgDesc(option)));
                    out.append("\\fP");
                    if (hasArg == LongOpt.OPTIONAL_ARGUMENT) {
                        out.append(']');
                    }
                }
            }
            out.println();
            printWrapped(out, 0, quoteDescription(getDescription(option)), 80);
            out.println();
        }
        
        out.append(getManFooter());
        out.flush();
    }

    public ManPrinter() {
        super();
    }

    public ManPrinter(String resourceName) {
        super(resourceName);
    }

    /**
     * @return the mAN_FOOTER
     */
    protected String getManFooter() {
        StringBuilder out = new StringBuilder();
        out.append(".SH SEE ALSO\n");
        for (String sa : seeAlsoList) {
            out.append(".BR ").append(sa).append(",\n");
        }
        out.append(
            ".br\n"+
            "More documentation is available in PDF and HTML format in the YajHFC FAQ and README files at\n");
       for (int i = 0; i < docLocations.length; i++) {
           out.append(".IR ").append(docLocations[i]);
           if (i < docLocations.length - 2) {
               out.append(" ,");
           } else if (i == docLocations.length - 2) {
               out.append("\nor");
           } // else append nothing
           out.append('\n');
       }
       out.append(
            "and on the homepage at\n"+
            ".IR http://www.yajhfc.de/\n"+
            ".SH AUTHOR\n"+
            "YajHFC was written by Jonas Wolz <jonas@yajhfc.de>.\n"+
            "\n");
       return out.toString();
    }
}
